package chapter11.dataExchange;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class DataExchangeFrame extends JFrame {
    public static final int TEXT_ROWS = 20;
    public static final int TEXT_COLUMNS = 40;
    private PasswordChooser dialog = null;
    private JTextArea textArea;


    public DataExchangeFrame() {
        var mbar = new JMenuBar();
        setJMenuBar(mbar);
        var fileMenu = new JMenu("File");
        mbar.add(fileMenu);

        var connectItem = new JMenuItem("Connect");
        connectItem.addActionListener(new ConnectAction());
        fileMenu.add(connectItem);

        var exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(event -> System.exit(0));
        fileMenu.add(exitItem);

        textArea = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
        add(new JScrollPane(textArea), BorderLayout.CENTER);
        pack();
    }


    private class ConnectAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (dialog == null) {
                dialog = new PasswordChooser();
            }
            dialog.setUser(new User("yourname", null));
            if (dialog.showDialog(DataExchangeFrame.this, "Connect")) {
                User u = dialog.getUser();
                textArea.append("user name = " + u.getName() + ", password = " + (new String(u.getPassword())) + "\n");


            }
        }
    }

}
